package service.impl;

import com.google.gson.Gson;
import dao.mapper.AuditMapper;
import models.Album;
import models.Audit;
import models.Comment;
import models.Photo;
import service.*;

import java.util.ArrayList;
import java.util.List;

public class AuditServiceImpl implements AuditService {
    public AuditMapper auditMapper;
    public UserService userService;
    public AlbumService albumService;
    public PhotoService photoService;
    public CommentService commentService;


    private List<Audit> auditList = new ArrayList<>();
    @Override
    public void setAuditList(List<Audit> auditList){};
    @Override
    public boolean addAudit(Audit audit){//增加一个Audit
        auditMapper.InsertAudit(audit);
        return true;};
    @Override
    public boolean response(String auditID, boolean result){return true;};
    @Override
    public Audit getAudit(Integer AuditID){
        Audit audit=auditMapper.GetAuditByFdid(AuditID);
        return null;} ;
    @Override
    public String getAuditList() {
        auditList = auditMapper.GetAuditList();

        // 使用Gson库将List<AuditEntity>转换为JSON格式的字符串
        Gson gson = new Gson();
        return gson.toJson(auditList);
    }
    @Override
    public void setDecision(Integer fdid, boolean result) {
        Audit audit = getAudit(fdid);
        if (result) {
            if (audit.getType().equals("album")) {
                Album album= albumService.getAlbumByAlbumid(audit.getTid());
                userService.banUser(album.getUid());
                albumService.deleteAlbum(audit.getTid());
            } else if (audit.getType().equals("photo")) {
                Photo photo = photoService.getPhotoByPid(audit.getTid());
                userService.banUser(photo.getUid());
                photoService.deletePhoto(audit.getTid());
            } else if(audit.getType().equals("comment")){
                Comment comment = commentService.getCommentByCid(audit.getTid());
                userService.banUser(comment.getUid());
                commentService.deleteComment(audit.getTid());
            }
            else{
                userService.unbanUser(audit.getUid());
            }
        }
        deleteAudit(fdid);

    }

    @Override
    public void deleteAudit(Integer fdid) {
        auditMapper.DeleteAudit(fdid);
    }
}
